package cn.ym.product.service.impl;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.product.domain.Product;
import cn.ym.product.mapper.ProductMapper;
import cn.ym.product.service.IProductService;
import cn.ym.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {



    @Autowired
    private ProductMapper productMapper;

    /**
     * 服务上架接口
     * @param ids
     * @param request
     * @return
     */
    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        for (Long id : ids) {
            Product product = productMapper.findOne(id);
            //已经是上架过的
            if (product.getState()==1){
                throw new BusinessException("该服务【"+product.getName()+"】已经是上架过的，请勿重复操作此功能!!!");
            }
            //就没有上架过的
            product.setState(1L);//1=上架
            productMapper.update(product);
        }
        return new AjaxResult();
    }


    /**
     * 服务下架：业务类
     * @param ids
     * @param
     */
    @Override
    public AjaxResult offsale(Long[] ids) {
        for (Long id : ids) {
            Product product = productMapper.findOne(id);
            if (product.getState()==0){
                throw new BusinessException("该服务【"+product.getName()+"】已经是下架过的，请勿重复操作此功能!!!");
            }
        }
        //1.修改状态，修改下架的时间
        productMapper.updateOffState(ids,new Date());
        return new AjaxResult();
    }




}
